
import wx
import config
from wx import xrc
from xrc.CommandAssistant_xrc import *

import win32gui
import win32con
import os



class CCommandAssistantPanel(xrcCommandAssistantPanel):
    def __init__(self, parent):
        super(CCommandAssistantPanel, self).__init__(parent)


        self.SetFinder(wx.Bitmap(config.BIT_MAP_FINDER_FULL))

        self.Bind(wx.EVT_BUTTON, self.OnClear, self.btn_clear)
        self.Bind(wx.EVT_BUTTON, self.OnSend, self.btn_send)
        self.sbm_finder.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.sbm_finder.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
        self.sbm_finder.Bind(wx.EVT_MOTION, self.OnMotion)

        self.wHandle=0

        self.Init()

    def SetFinder(self,bitmap):
        self.sbm_finder.SetBitmap(bitmap)

    def OnClear(self,event):
        print "clear"

    def OnSend(self,event):
        if self.wHandle == 0:
            dlg = wx.MessageDialog(self, 'Please select a target window!',
                           'Warning Message',
                           wx.OK | wx.ICON_INFORMATION
                           #wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION
                           )
            dlg.ShowModal()
            dlg.Destroy()
            return

        pos = self.tc_command.GetInsertionPoint()

        (colNo,lineNo) = self.tc_command.PositionToXY(pos)

        if self.tc_command.GetLineLength(lineNo) == 0:
            return

        command = self.tc_command.GetLineText(lineNo)

        for c in command:
            win32gui.PostMessage(self.wHandle,win32con.WM_CHAR, ord(c), 0)

        win32gui.PostMessage(self.wHandle,win32con.WM_KEYDOWN,win32con.VK_RETURN, 0)


        #after send message to target window, move the insertion point to the next line
        pos = self.tc_command.XYToPosition(0,lineNo+1)
        self.tc_command.SetInsertionPoint(pos)
        self.tc_command.SetFocus()


    def OnLeftDown(self, event):
        self.sbm_finder.CaptureMouse()
        self.SetFinder(wx.Bitmap(config.BIT_MAP_FINDER_EMPTY))

        #use the finder pic as the cursor
        cursor=wx.Cursor(config.CURSOR_FINDER, wx.BITMAP_TYPE_CUR)
        self.SetCursor(cursor)

    def OnLeftUp(self, event):
        if self.sbm_finder.HasCapture():
            self.sbm_finder.ReleaseMouse()
            self.sbm_finder.SetBitmap(wx.Bitmap(config.BIT_MAP_FINDER_FULL))

            #reset the cursor to the default one
            self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))

            p=self.sbm_finder.ClientToScreenXY(event.GetX(), event.GetY())
            self.wHandle = win32gui.WindowFromPoint(p)

    def OnMotion(self, event):
        if not event.Dragging() and not event.LeftIsDown():
            return
        event.Skip()

    def Init(self):
        #when init the panel, should check if the command.txt exists, if yes, load it
        if os.path.exists(config.COMMAND_FILE_NAME):
            self.tc_command.LoadFile(config.COMMAND_FILE_NAME)

    def OnOpen(self, event):
        #the open action for command panel, it just open a file that contain the command

        dlg = wx.FileDialog(
            self, message="Choose a file",
            defaultDir=os.getcwd(), 
            defaultFile="",
            wildcard=r"Text Documents (*.txt) | *.txt",
            style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
            )

        if dlg.ShowModal() == wx.ID_OK:
            # This returns a Python list of files that were selected.
            path = dlg.GetPath()

            self.tc_command.LoadFile(path)

    def OnClose(self, event):
        #if not self.tc_command.IsEmpty():
        #    self.tc_command.SaveFile(config.COMMAND_FILE_NAME)
        pass

    def OnSave(self, event):
        if not self.tc_command.IsEmpty():
            self.tc_command.SaveFile(config.COMMAND_FILE_NAME)

    def OnSaveAs(self, event):
        if self.tc_command.IsEmpty():
            return

        dlg = wx.FileDialog(
            self, message="Save file as ...", defaultDir=os.getcwd(),
            defaultFile="", wildcard=r"Text Documents (*.txt) | *.txt | All Files (*.*) | *.*", style=wx.SAVE
            )

        if dlg.ShowModal() == wx.ID_OK:
            # This returns a Python list of files that were selected.
            path = dlg.GetPath()

        self.tc_command.SaveFile(path)


    def OnClear(self, event):  # wxGlade: CCmdAssistantPanel.<event_handler>
        self.tc_command.Clear()

# end of class CCmdAssistantPanel

class CCommandAssistantPanelXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)

    def CanHandle(self, node):
        return self.IsOfClass(node, "CCommandAssistantPanel")

    def DoCreateResource(self):
        panel = CCommandAssistantPanel(self.GetParentAsWindow())

        self.SetupWindow(panel)

        self.CreateChildren(panel)

        return panel
